let baseURL = version();
function version(){
  let version = __wxConfig.envVersion;
  switch (version){
    case 'develop':
      return getApp().globalData.api_url;
      break;
    default:
      return getApp().globalData.api_url;
  }
}

export interface IMessage{
  role: 'user' | 'system' | 'assistant',
  content: String
}

interface header {
  'Content-Type': string,
  Authorization?: string
}

interface params {
  data: {
    messages: IMessage[],
    modelId: String,
    stream?: Boolean
  },
  url?: string,
  method?: "GET" | "POST",
  success?: Function,
  fail?: Function,
  complete?: Function
}

export default (params: params) => {
  let data = params.data;
  let header:header = {
    "Content-Type": "application/json"
  }
  const token = wx.getStorageSync('token')
  if(token){
    header['Authorization'] = `Bearer ${token}`
  }
  return wx.request({
    url: baseURL + params.url,
    method: "POST",
    header,
    enableChunked: true,
    data: {
      stream: true,
      messages: data.messages,
      modelId: data.modelId
    },
    success: (res) => {
      params.success && params.success(res)
    },
    fail: (res) => {
      params.fail && params.fail(res)
    },
    complete: (res) => {
      params.complete && params?.complete(res)
    }
  })
}
